package com.rjt.article.service.impl;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.rjt.article.model.ArticleCommentModel;
import com.rjt.article.repository.ArticleCommentRepository;
import com.rjt.article.service.ArticleCommentService;
import com.rjt.common.util.StaticMethod;
import com.rjt.common.util.TextUtil;
@Service
public class ArticleCommentServiceImpl implements ArticleCommentService{

	@Autowired
	private ArticleCommentRepository articleCommentRepository;
	@Override
	public List<ArticleCommentModel> findByRId(String rId) {
		// TODO Auto-generated method stub
		 ArticleCommentModel articleCommentModel = new ArticleCommentModel();
		 articleCommentModel.setrId(rId);
		 articleCommentModel.setIsDel(0);
		 articleCommentModel.setIsShow(1);
		 ExampleMatcher matcher = ExampleMatcher.matching()
		            .withIgnorePaths("id");
		 Example<ArticleCommentModel> example = Example.of(articleCommentModel,matcher);
		 return	articleCommentRepository.findAll(example);
	}
	@Override
	public String save(String content, HttpSession session,String id,HttpServletRequest request) {
		// TODO Auto-generated method stub
		String flag = "success";
		try {
			ArticleCommentModel articleCommentModel = new ArticleCommentModel();
			articleCommentModel.setContent(content);
			articleCommentModel.setrId(id);
			articleCommentModel.setCreateAt(new Date());
			articleCommentModel.setIp(TextUtil.getRemortIP(request));
			articleCommentModel.setLikeNum(0);
			articleCommentModel.setReplyLikeNum(0);
			articleCommentModel.setIsDel(0);
			articleCommentModel.setIsShow(0);
			articleCommentModel.setIsTop(0);
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					if (cookies[i].getName().equals("nickname")) {
						articleCommentModel.setNickName(URLDecoder.decode(cookies[i].getValue(),"UTF-8"));
					}
					if (cookies[i].getName().equals("headimgurl")) {
						articleCommentModel.setHeadImg(cookies[i].getValue());
					}
				}
			}else{
				articleCommentModel.setHeadImg("http:\\/\\/thirdwx.qlogo.cn\\/mmopen\\/vi_32\\/Q0j4TwGTfTIUfLv3p0jDT6MlqM8icibibic7QTFmXxsxUYNV7nrKVfX7icnicBKY4fpjSSvd4RCdu7gv5ONAk3qTXAJw\\/132");
				articleCommentModel.setNickName("小黑");
			}
			articleCommentRepository.save(articleCommentModel);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag = "error";
		}
		return flag;
	}
	@Override
	public Page<ArticleCommentModel> getList(HttpServletRequest request) {
		// TODO Auto-generated method stub
		int curPage = StaticMethod.nullObject2int(request.getParameter("page")) <= 0 ? 1 : 
			StaticMethod.nullObject2int(request.getParameter("page"));
		int pageSize = StaticMethod.nullObject2int(request.getParameter("limit")) <= 0 ? 10 : 
			StaticMethod.nullObject2int(request.getParameter("limit"));
		 PageRequest pageRequest = new PageRequest(curPage-1, pageSize,Direction.DESC, "createAt");
		 ArticleCommentModel articleCommentModel = new ArticleCommentModel();
		 articleCommentModel.setrId(request.getParameter("rId"));
		 articleCommentModel.setIsDel(0);
		 ExampleMatcher matcher = ExampleMatcher.matching()
		            .withIgnorePaths("id");
		 Example<ArticleCommentModel> example = Example.of(articleCommentModel,matcher);
		 return	articleCommentRepository.findAll(example, pageRequest);
		 
	}
	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		 articleCommentRepository.delete(id);
	}
	@Override
	public ArticleCommentModel findOne(Integer id) {
		// TODO Auto-generated method stub
		return  articleCommentRepository.findOne(id);
	}
	@Override
	public String update(ArticleCommentModel articleComment) {
		// TODO Auto-generated method stub
		String flag="success";
		try {
			ArticleCommentModel articleCommentDB = articleCommentRepository.findOne(articleComment.getId());
			
			articleComment.setIsDel(0);
			articleComment.setrId(articleCommentDB.getrId());
			articleComment.setCreateAt(articleCommentDB.getCreateAt());
			articleComment.setReplyCreateAt(articleComment.getReplyContent()==null?articleCommentDB.getReplyCreateAt():new Date());
			articleComment.setIp(articleCommentDB.getIp());
			
			articleCommentRepository.save(articleComment);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			flag="error";
		}
		return flag;
	}
	
}
